package com.qipay.sp.excutor.ronghui;

import com.qipay.baas.model.Order;
import com.qipay.scalar.type.ApiStatusEnum;
import com.qipay.scalar.type.ApiTypeEnum;
import com.qipay.sp.excutor.AbstractExcutor;
import com.qipay.utils.JsonUtils;
import com.qipay.utils.SignSortMD5Utils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

@Component
public class RonghuiUnionNetExcutor extends AbstractExcutor {

    private final static String CONFIG_KEY_NAME="key";
    private final static String CONFIG_MCHID_NAME="mch_id";
    private final static String CONFIG_IP_NAME="ip";
    private final static String SIGN_NAME="sign";
    private final static String API_PARAM_SERVER_CALLBACK_URL="serverCallbackUrl";
    private final static String API_PARAM_TRX_TYPE="trxType";
    private final static String API_PARAM_MERCHANT_NO="merchantNo";
    private final static String API_PARAM_ORDER_NUM="orderNum";
    private final static String API_PARAM_AMOUNT="amount";
    private final static String API_PARAM_BANK_ID="bankId";
    private final static String API_PARAM_GOODS_NAME="goodsName";
    private final static String API_PARAM_ORDER_IP="orderIp";
    private final static String API_PARAM_ENCRYPT="encrypt";
    private final static String API_URL="http://trx.ronghuijinfubj.com/middlepaytrx/online/pay";

    @Override
    protected ApiTypeEnum getApiTypeEnum() {
        return ApiTypeEnum.UNION_NET;
    }

    @Override
    protected Map<String, Object> getApiParams(Order order, Map<String, Object> spconfig, Map<String, Object> extendOrderParams) {
        Map<String, Object> apiParams = new HashMap<>();
        apiParams.put(API_PARAM_ORDER_NUM, order.getTradeNo());
        apiParams.put(API_PARAM_GOODS_NAME, order.getBody());
        apiParams.put(API_PARAM_AMOUNT, new BigDecimal(order.getAmount()).divide(new BigDecimal(100)));

        apiParams.put(API_PARAM_BANK_ID, extendOrderParams.get("bank_id"));
        return apiParams;
    }

    @Override
    protected Map<String, Object> run(Map<String, Object> spconfig, Map<String, Object> params) throws Exception {

        Object key = getVal(spconfig, CONFIG_KEY_NAME);

        Map<String,Object> sortParams = new LinkedHashMap<>();
        sortParams.put(API_PARAM_TRX_TYPE,getVal(params, API_PARAM_TRX_TYPE));
        sortParams.put(API_PARAM_MERCHANT_NO,getVal(spconfig, CONFIG_MCHID_NAME));
        sortParams.put(API_PARAM_ORDER_NUM,getVal(params, API_PARAM_ORDER_NUM));
        sortParams.put(API_PARAM_AMOUNT,getVal(params, API_PARAM_AMOUNT));
        sortParams.put(API_PARAM_BANK_ID,getVal(params, API_PARAM_BANK_ID));
        sortParams.put(API_PARAM_GOODS_NAME,getVal(params, API_PARAM_GOODS_NAME));
        sortParams.put(API_PARAM_SERVER_CALLBACK_URL,getVal(params, API_PARAM_SERVER_CALLBACK_URL));
        sortParams.put(API_PARAM_ORDER_IP,getVal(spconfig, CONFIG_IP_NAME));
        sortParams.put(API_PARAM_ENCRYPT,getVal(params, API_PARAM_ENCRYPT));

        String sign = SignSortMD5Utils.getSignBySeparator(sortParams,"#",key.toString());
        sortParams.put(SIGN_NAME, sign);

        String result = sendPostByKeyValue(sortParams, RonghuiUnionNetExcutor.API_URL);
        if(StringUtils.isBlank(result)){
            throw new Exception("请求ronghui网关接口失败");
        }

        Map<String, Object> responseMap = JsonUtils.toMap(result);
        Map<String, Object> resultMap = new HashMap<>();

        if(responseMap == null || !responseMap.containsKey("respCode") || responseMap.get("respCode") == null )
        {
            logger.error("请求ronghui网关接口失败:" + result);

            if(responseMap.containsKey("retCode")){
                resultMap.put("result_code", "1");
                resultMap.put("err_code", ApiStatusEnum.SP_ERR.getCode());
                resultMap.put("err_msg", responseMap.get("retMsg"));
                return  resultMap;
            }

            return null;
        }

        if(!(responseMap.get("respCode").toString().equals("0000") || responseMap.get("respCode").toString().equals("1000"))) {
            logger.error("请求ronghui网关接口业务执行失败:" + result);
            resultMap.put("result_code", "1");
            resultMap.put("err_code", ApiStatusEnum.SP_ERR.getCode());
            resultMap.put("err_msg", responseMap.get("respMsg"));
            return resultMap;
        }

        resultMap.put("result_code", "0");
        resultMap.put("pay_info", responseMap.get("url"));
        return resultMap;
    }
}
